package itsm.isperp.module.service.project;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.module.entity.project.ProjCostItem;
import itsm.isperp.module.repository.project.ProjCostItemMapper;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 项目预算管理服务类
 * 
 * @author lizx
 * @date 2013-3-2
 * @version V1.0
 * 
 */
@Service
public class ProjCostItemService extends BaseService<ProjCostItem> {

	@Autowired
	protected ProjCostItemMapper projProjectCostItemMapper;

	@Override
	public ProjCostItemMapper getRepository() {
		return this.projProjectCostItemMapper;
	}

	/**
	 * 查询项目中的预算项
	 * 
	 * @param projectId
	 *            项目id
	 * @param costTemplateId
	 *            预算模板id
	 * @return 项目预算项
	 */
	public List<ProjCostItem> getProjectCost(String projectId,
			String costTemplateId) {
		return this.getRepository().findProjectCost(projectId, costTemplateId);
	}

	/**
	 * 删除项目的预算项
	 * 
	 * @param projectId
	 *            项目id
	 */
	public void deleteByProjectId(String projectId) {
		this.getRepository().deleteByProjectId(projectId);
	}

	/**
	 * 批量插入项目预算项数据
	 * 
	 * @param costItems
	 *            项目预算 项列表
	 * @param projectId
	 *            项目id
	 * @param costTemplateId
	 *            预算模板id
	 */
	public void batchInsert(List<String[]> costAmounts, String projectId,
			String projCostTemplateId) {
		
		
		
		String sql = "INSERT INTO proj_cost_item(id,amount,proj_project_id,proj_cost_template_id,proj_cost_template_node_id)VALUES(?,?,?,?,?)";

		JdbcTemplate jdbcTemplate = ContextHolder
				.getSpringBean("jdbcTemplate");
		List<Object[]> list = new ArrayList<>();

		if (costAmounts != null && costAmounts.size() > 0) {
			for (String[] m : costAmounts) {
				Object[] arg = new Object[5];
				arg[0] = HibernateUUIDGenerator.generate();
				arg[1] = m[0];
				arg[2] = projectId;
				arg[3] = projCostTemplateId;
				arg[4] = m[1];

				list.add(arg);
			}

			jdbcTemplate.batchUpdate(sql, list);
		}
		
	}
}
